<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\UserLoginException;
use App\Requests\Admin\LoginRequest;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    public function showLoginForm()
    {
        if (Auth::check()) {
            return redirect('admin');
        } else {
            return view('admin.login.login');
        }
    }
    public function login(LoginRequest $request){
        $request->scene('SubmitLogin')->validate();
        $data = $request->all();
        $attempt = [
            'username'  => $data['name'],
            'password' => $data['password'],
            'status'   => 1,
        ];
        if (Auth::guard('admin')->attempt($attempt)) {
            return redirect('admin');
        } else {
            throw new UserLoginException();
//            return redirect('admin/login')->with('error', '用户名或密码错误或账号禁用');
        }
    }

    public function logout()
    {
        Auth::guard('admin')->logout();
        return redirect('admin/login');
    }
}
